/*
 * Copyright  bocmbci
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { CordovaBridge } from '../CordovaBridge';
import { ExposedJsApi } from '../ExposedJsApi';
import webview from '@ohos.web.webview';

/**
 * 原生api入口，供js调用
 */
export class SystemExposedJsApi implements ExposedJsApi{
  cordovaBridge: CordovaBridge;

  constructor(_cordovaBridge, _webViewController) {
    this.cordovaBridge = _cordovaBridge;
  }

  exec = (params): string => {
    params = JSON.parse(params);
    if(this.cordovaBridge != null && this.cordovaBridge != undefined){
      return this.cordovaBridge.jsExec(params.bridgeSecret, params.service, params.action,
        params.callbackId, params.argsJson);
    }
  }

  setNativeToJsBridgeMode = (bridgeSecret: number, value: number) => {
    if(this.cordovaBridge != null && this.cordovaBridge != undefined){
      this.cordovaBridge.jsSetNativeToJsBridgeMode(bridgeSecret, value);
    }
  }

  retrieveJsMessages = (bridgeSecret: number, fromOnlineEvent: boolean) => {
    if(this.cordovaBridge != null && this.cordovaBridge != undefined){
      return this.cordovaBridge.jsRetrieveJsMessages(bridgeSecret, fromOnlineEvent);
    }
  }
}